package com.zmm.study.order.interceptor;

import com.zmm.common.base.model.Result;
import com.zmm.common.base.model.ResultCode;
import com.zmm.util.auth.constant.AuthConstant;
import com.zmm.util.auth.interceptor.AbstractAuthInterceptor;
import com.zmm.util.auth.model.AuthParam;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 900045
 * @description:
 * @name OrderAuthInterceptor
 * @date By 2021-10-19 19:05:05
 */
@Component
public class OrderAuthInterceptor extends AbstractAuthInterceptor implements HandlerInterceptor {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String requestURI = request.getRequestURI();
		AntPathMatcher antPathMatcher = new AntPathMatcher();
		if (checkUrl(requestURI, antPathMatcher)) {
			return HandlerInterceptor.super.preHandle(request, response, handler);
		}
		String token = request.getHeader(AuthConstant.HEADER_TOKEN_KEY);
		AuthParam param = new AuthParam(request, response);
		if (token == null){
			return returnResp(param.getResponse(), Result.error(ResultCode.USER_NOT_LOGIN));
		}
		return false;
	}

	/**
	 * URL 拦截过滤
	 * @author: 900045
	 * @date: 2021-10-21 14:36:12
	 * @throws 
	 * @param requestURI: 
	 * @param antPathMatcher: 
	 * @return: boolean
	 **/
	private boolean checkUrl(String requestURI, AntPathMatcher antPathMatcher) {
		boolean match1 = antPathMatcher.match("/order/test/create/order/**", requestURI);

		boolean match2 = antPathMatcher.match("/order/test/close/all/order/**", requestURI);

		boolean match3 = antPathMatcher.match("/order/test/finish/order/**", requestURI);

		if (match1 || match2 || match3) {
			return true;
		}
		return false;
	}
}
